<?php

class Application_Model_UserMapper
{
    protected $_dbTable;

    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Application_Model_DbTable_User');
        }
        return $this->_dbTable;
    }

    public function save(Application_Model_User $user)
    {
        $data = array(
            'first_name'       => $user->getFirstName(),
            'last_name'        => $user->getLastName(),
            'male'             => ($user->getMale() ? 1 : 0),
            'email'            => $user->getEmail(),
            'age'              => $user->getAge(),
            'country'          => $user->getCountry(),
            'login'            => $user->getLogin(),
            'activated'        => $user->getActivated(),
            'activation_hash'  => $user->getActivationHash(),
        );

        if ($user->getPassword()) {
            $data['password'] = $user->getPassword();
        }

        if (null === ($id = $user->getId())) {
            return $this->getDbTable()->insert($data);
        } else {
            return $this->getDbTable()->update($data, array('id = ?' => $id));
        }
    }

    public function findByEmail($email) {
        $result = false;

        $dbtable = $this->getDbTable();
        $where   = $dbtable->getAdapter()->quoteInto('email = ?', $email);
        $record  = $dbtable->fetchRow($where);

        if (!empty($record->id)) {
            $model = new Application_Model_User();
            $model->setId($record->id);
            $model->setFirstName($record->first_name);
            $model->setLastName($record->last_name);
            $model->setMale($record->male);
            $model->setEmail($record->email);
            $model->setAge($record->age);
            $model->setCountry($record->country);
            $model->setLogin($record->login);
            $model->setActivated($record->activated);
            $result = $model;
        }

        return $result;
    }

    public function findByLogin($login) {
        $result = false;

        $dbtable = $this->getDbTable();
        $where   = $dbtable->getAdapter()->quoteInto('login = ?', $login);
        $record  = $dbtable->fetchRow($where);

        if (!empty($record->id)) {
            $model = new Application_Model_User();
            $model->setId($record->id);
            $model->setFirstName($record->first_name);
            $model->setLastName($record->last_name);
            $model->setMale($record->male);
            $model->setEmail($record->email);
            $model->setAge($record->age);
            $model->setCountry($record->country);
            $model->setLogin($record->login);
            $model->setActivated($record->activated);
            $result = $model;
        }

        return $result;
    }

    public function createFromArray($data) {
        return $this->getDbTable()->insert($data);
    }

    public function activateByHash($hash)
    {

        $dbtable = $this->getDbTable();
        $where   = $dbtable->getAdapter()->quoteInto('activation_hash = ?', $hash);
        $record  = $dbtable->fetchRow($where);

        if (!empty($record->id))
        {
            $update = array(
                'activated'       => 1,
                'activation_hash' => null,
            );
            return $this->getDbTable()->update($update, array('id = ?' => $record->id));
        }

        return false;

    }

    public function loadByEmailAndPassword($email, $password) {
        $dbtable = $this->getDbTable();

        $select = $dbtable->select(); 
        $select->where('email = ?', $email)
               ->where('activated = 1')
               ->where('password = ?', $password);

        $record  = $dbtable->fetchRow($select);

        if ($record && $record->id)
        {
            $model = new Application_Model_User();
            $model->setId($record->id);
            $model->setFirstName($record->first_name);
            $model->setLastName($record->last_name);
            $model->setMale($record->male);
            $model->setEmail($record->email);
            $model->setAge($record->age);
            $model->setCountry($record->country);
            $model->setLogin($record->login);
            $model->setActivated($record->activated);
            return $model;
        }

        return false;

    }

    public function createResetPasswordHash(Application_Model_User $user)
    {
        $hash   = md5($user->getId() . $user->getFirstName() . $user->getEmail() . uniqid());
        $update = array(
            'change_password_hash' => $hash
        );
        return $this->getDbTable()->update($update, array('id = ?' => $user->getId())) ? $hash : false;
    }

    public function clearResetPasswordHash(Application_Model_User $user)
    {
        $update = array(
            'change_password_hash' => null
        );
        return $this->getDbTable()->update($update, array('id = ?' => $user->getId()));
    }

    public function findByResetPasswordHash($hash)
    {
        $result = false;

        $dbtable = $this->getDbTable();
        $where   = $dbtable->getAdapter()->quoteInto('change_password_hash = ?', $hash);
        $record  = $dbtable->fetchRow($where);

        if (!empty($record->id)) {
            $model = new Application_Model_User();
            $model->setId($record->id);
            $model->setFirstName($record->first_name);
            $model->setLastName($record->last_name);
            $model->setMale($record->male);
            $model->setEmail($record->email);
            $model->setAge($record->age);
            $model->setCountry($record->country);
            $model->setLogin($record->login);
            $model->setActivated($record->activated);
            $result = $model;
        }

        return $result;
    }

}